<?php 
require_once 'userManagement.php';
require_once 'settingsManagement.php';
require_once 'profits.php';
$db = new DataBase();
$config = new Config();
$db->connect($config -> getHost(), $config -> getPort(), $config->getDbname(), $config->getUser(), $config->getPass());
$newRestaurant=new Restaurant($db);
$editRestaurant=new Restaurant($db);
$statement=null;
?>
<!DOCTYPE html>
<html>
<head>
  <title>Panel Administratora</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8; width=device-width, initial-scale=1.0" >
  <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<?php
if (isset($_GET['rM'])) {	  
  if($_GET['rM']==1){
?>
	<div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-warning">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-danger">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a href="../" type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
		    <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-danger btn-block" style="width: 270px;">Znajdź restaurację</a>
		    <a href="restaurantManagement.php?rM=2" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj restaurację</a>
			<a href="restaurantManagement.php?rM=3" type="button" class="btn btn-warning btn-block" style="width: 270px;">Edytuj restaurację</a>
			<a href="restaurantManagement.php?rM=4" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj oczekujące restauracje</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-search form-inline" action="restaurantManagement.php?rM=1" method="POST">
			  <legend><center>Znajdź restaurację</center></legend>
			  <center>			  
			    <select class="input" name="findRestaurantFilterChooser">
				  <option>Nazwa restauracji</option>
				  <option>Właściciel</option>
				  <option>Wszystkie</option>
			    </select>
			  <input type="text" class="input search-query" name="findRestaurantFilter">
			  <button type="submit" class="btn-primary" name="findRestaurant">Szukaj</button>
			  </center>
			</form>
		  </div>
		  <div class="well">
			<form class="form-group" style="height: 290px">
			  <legend><center>Restauracje:</center></legend>
<?php
	if(isset($_REQUEST['findRestaurant'])) {
		if(isset($_POST['findRestaurantFilter'])){
			if(isset($_POST['findRestaurantFilterChooser'])){
				$db = new DataBase();
				$config = new Config();
				$db->connect($config -> getHost(), $config -> getPort(), $config->getDbname(), $config->getUser(), $config->getPass());
				if($_POST['findRestaurantFilterChooser']=='Nazwa restauracji'){
					$statement=$db->query('select id, rname, location from restaurants where rname="'.$_POST["findRestaurantFilter"].'"');
				}
				if($_POST['findRestaurantFilterChooser']=='Właściciel'){
					$statement=$db->query('select r.id, r.rname, r.location, u.name, c.name from restaurants r, users u, chains c where u.name="'.$_POST['findRestaurantFilter'].'" and r.owner=u.id and c.owner=u.id');
				}
				if($_POST['findRestaurantFilterChooser']=='Wszystkie'){
					$statement=$db->query('select id, rname, location from restaurants');
				}
			}
		}
		echo('<label class="col-sm-4 control-label">ID restauracji</label>
			  <label class="col-sm-4 control-label"><center>Nazwa restauracji</center></label>
			  <label class="col-sm-4 control-label"><center>Adres restauracji</center></label><br><br>');
	foreach($statement as $row)
		{
          echo('<label class="col-sm-4 control-label">'.$row['id'].'</label>');
		  echo('<label class="col-sm-4 control-label"><center>'.$row['rname'].'</center></label>');
		  echo('<label class="col-sm-4 control-label"><center>'.$row['location'].'</center></label>');
		  
		}
      $statement->closeCursor();
	}
?>			  <label class="col-sm-9 control-label">
			  </label>
			</form>
		  </div>
		</div>
	</div>
  </div>
  </body>
  </html>
<?php
  }
  else if($_GET['rM']==2){
?>
	<div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-warning">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-danger">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
		    <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning btn-block" style="width: 270px;">Znajdź restaurację</a>
		    <a href="restaurantManagement.php?rM=2" type="button" class="btn btn-danger btn-block" style="width: 270px;">Dodaj restaurację</a>
			<a href="restaurantManagement.php?rM=3" type="button" class="btn btn-warning btn-block" style="width: 270px;">Edytuj restaurację</a>
			<a href="restaurantManagement.php?rM=4" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj oczekujące restauracje</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		 <div class="well">
		    <form class="form-horizontal" role="form" action="restaurantManagement.php?rM=2" method="POST">
			  <div class="form-group"><br>
				<legend><center>Formularz dodawania nowej restauracji</center></legend>
			    <label class="col-sm-2 control-label">Nazwa:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" placeholder="Nazwa Restauracji" name="newRestaurantName">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Właściciel:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" placeholder="Właściciel" name="newRestaurantOwner">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Adres:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" placeholder="Adres" name="newRestaurantAddress">
			    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Kod pocztowy:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" placeholder="Kod pocztowy" name="newRestaurantPostCode">
			    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Miasto:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" placeholder="Miasto" name="newRestaurantCity">
			    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Sieć restauracjii:</label>
			    <div class="col-sm-10">
			      <select class="input" name="newRestaurantChain">
				    <option>Brak</option>
<?php
$statement=$db->query('select name from chains');
foreach($statement as $row)
      {
          echo('<option>'.$row['name'].'</option>');					
      }
      $statement->closeCursor();
?>
			      </select>
			    </div>
			  </div>
			  <div class="form-group">
			    <div>
				  <center>
				    <button type="submit" class="btn btn-primary" name="addRestaurantButton">Dodaj restaurację</button>
			      </center>
				</div>
			  </div>
		    </form>
<?php
	if(isset($_REQUEST['addRestaurantButton'])) {
		$restaurant= new Restaurant($db);
		$temp;
		if(isset($_POST['newRestaurantName'])){
			$restaurant->setName($_POST['newRestaurantName']);
		}
		if(isset($_POST['newRestaurantOwner'])){
			$statement=$db->query('select id from users where name="'.$_POST['newRestaurantOwner'].'"');
			foreach($statement as $row)
			{
				$restaurant->setOwner($row['id']);
			}
			$statement->closeCursor();
		}
		if(isset($_POST['newRestaurantAddress'])){
			if(isset($_POST['newRestaurantPostCode'])){
				if(isset($_POST['newRestaurantCity'])){
					$temp=$_POST['newRestaurantAddress'].', '.$_POST['newRestaurantPostCode'].' '.$_POST['newRestaurantCity'];
					$restaurant->setLocation($temp);
				}
			}
		}
		if($_POST['newRestaurantChain']){
			$statement=$db->query('select id from chains where name="'.$_POST['newRestaurantChain'].'"');
				foreach($statement as $row)
					{
						$restaurant->attachToChain($row['id']);
					}
					$statement->closeCursor();
		}		
		$restaurant->save();
	}				
?>						
		  </div>
		</div>
	</div>
  </div>
  </body>
  </html>
<?php
  }
  else if($_GET['rM']==3){
?>
  <div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-warning">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-danger">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
		    <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning btn-block" style="width: 270px;">Znajdź restaurację</a>
		    <a href="restaurantManagement.php?rM=2" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj restaurację</a>
			<a href="restaurantManagement.php?rM=3" type="button" class="btn btn-danger btn-block" style="width: 270px;">Edytuj restaurację</a>
			<a href="restaurantManagement.php?rM=4" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj oczekujące restauracje</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-search form-inline" action="restaurantManagement.php?rM=3" method="POST">
			  <legend><center>Edytuj restaurację</center></legend>
			  <center>			  
			    <select class="input" name="editRestaurantFilterChooser">
				  <option>Nazwa restauracji</option>
				  <option>ID</option>
			    </select>
			  <input type="text" class="input search-query" name="editRestaurantFilter">
			  <button type="submit" class="btn-primary" name="editRestaurantButton">Szukaj</button>
			  <button type="submit" class="btn-primary" name="deleteRestaurantButton">Usuń</button>
			  </center>
			</form>
		  </div>
<?php		  
	if(isset($_REQUEST['editRestaurantButton'])) {
		if(isset($_POST['editRestaurantFilter'])){
			if(isset($_POST['editRestaurantFilterChooser'])){
				if($_POST['editRestaurantFilterChooser']=='ID'){
					$statement=$db->query('select * from restaurants where id="'.$_POST["editRestaurantFilter"].'"');
				}
				if($_POST['editRestaurantFilterChooser']=='Nazwa restauracji'){
					$statement=$db->query('select * from restaurants where rname="'.$_POST["editRestaurantFilter"].'"');
				}
				foreach($statement as $row)
				  {
					$editRestaurant->setName($row['rname']);
					$editRestaurant->attachToChain($row['chain']);
					$editRestaurant->setOwner($row['owner']);
					$editRestaurant->setLocation($row['location']);
				  }
				$statement->closeCursor();
			}
		}
	}	 
	if(isset($_REQUEST['deleteRestaurantButton'])) {
		if(isset($_POST['editRestaurantFilter'])){
			if(isset($_POST['editRestaurantFilterChooser'])){
				if($_POST['editRestaurantFilterChooser']=='ID'){
					$db->exec('delete from restaurants where id="'.$_POST["editRestaurantFilter"].'"');
				}
				if($_POST['editRestaurantFilterChooser']=='Nazwa restauracji'){
					$db->query('delete from restaurants where rname="'.$_POST["editRestaurantFilter"].'"');
				}
			}
		}
	}
		
?>		  
		 <div class="well">
		    <form class="form-horizontal" role="form" action="restaurantManagement.php?rM=3" method="POST">
			  <div class="form-group"><br>
				<legend><center>Formularz edytowania restauracji</center></legend>
			    <label class="col-sm-2 control-label">Nazwa:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" placeholder="Nazwa Restauracji" name="editRestaurantName" value="
<?php
	echo($editRestaurant->getName());
?>				  
				  ">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Właściciel:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" placeholder="Właściciel" name="editRestaurantOwner" value="
<?php
	$statement=$db->query('select name from users where id="'.$editRestaurant->getOwner().'"');
				foreach($statement as $row)
				  {
					echo($row['name']);
				  }
				$statement->closeCursor();
?>		  				  
				  ">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Adres:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" placeholder="Adres" name="editRestaurantAddress" value="
<?php
	echo($editRestaurant->getLocation());
?>	
				  ">
			    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Sieć restauracji:</label>
			    <div class="col-sm-10">
			      <select class="input" name="editRestaurantChain">
				    <option>Brak</option>
<?php
$statement=$db->query('select name from chains');
foreach($statement as $row)
      {
          echo('<option>'.$row['name'].'</option>');					
      }
      $statement->closeCursor();
?>
			      </select>
			    </div>
			  </div>
			  <div class="form-group">
			    <div>
				  <center>
				    <button type="submit" class="btn btn-primary" name="editRestaurantButton">Edytuj restaurację</button>
			      </center>
				</div>
			  </div>
		    </form>
<?php
if(isset($_REQUEST['editRestaurantButton'])) {
		if(isset($_POST['editRestaurantChain'])){
			$statement=$db->query('select id from chains where name="'.$_POST['editRestaurantChain'].'"');
			foreach($statement as $row)
				  {
					$editRestaurant->attachToChain($row['id']);
				  }
				$statement->closeCursor();
		}
		if(isset($_POST['editRestaurantName'])){
			$editRestaurant->setName($_POST['editRestaurantName']);
		}
		if(isset($_POST['editRestaurantOwner'])){
			$statement=$db->query('select id from users where name="'.$_POST['editRestaurantOwner'].'"');
			foreach($statement as $row)
				  {
					$editRestaurant->setOwner($row['id']);
				  }
				$statement->closeCursor();
		}
		if(isset($_POST['editRestaurantAddress'])){
			$editRestaurant->setLocation($_POST['editRestaurantAddress']);
		}
	}	
?>		
		  </div>
		</div>
	  </div>
	</div>
  </body>
  </html>
<?php
  }
  else{
?>
	<div class="container" id="main-container">
	<div class="row" style="height: 150px;">
	  <div class="col-md-12"></div>
	</div>
	<div class="row">
	  <div class="col-md-3" id="top-left-menu"></div>
	  <div class="col-md-9" id="top-right-menu">
	    <div class="btn-group btn-group-justified">
		  <a href="userManagement.php?uM=1" type="button" class="btn btn-warning">Zarządzaj użytkownikami</a>
		  <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-danger">Zarządzaj restauracjami</a>
		  <a href="settingsManagement.php?sM=1" type="button" class="btn btn-warning">Zarządzaj ustawieniami</a>
		  <a href="profits.php?p=1" type="button" class="btn btn-warning">Podgląd zysków</a>
		  <a type="button" class="btn btn-warning">Wyloguj</a>
		</div>
	  </div>
	</div>
    <div class="row">
        <div class="col-md-3" id="left-menu">
		  <div class="btn-group-vertical btn-group-block">
		    <a href="restaurantManagement.php?rM=1" type="button" class="btn btn-warning btn-block" style="width: 270px;">Znajdź restaurację</a>
		    <a href="restaurantManagement.php?rM=2" type="button" class="btn btn-warning btn-block" style="width: 270px;">Dodaj restaurację</a>
			<a href="restaurantManagement.php?rM=3" type="button" class="btn btn-warning btn-block" style="width: 270px;">Edytuj restaurację</a>
			<a href="restaurantManagement.php?rM=4" type="button" class="btn btn-danger btn-block" style="width: 270px;">Dodaj oczekujące restauracje</a>
		  </div>
		</div>
        <div class="col-md-9" id="body">
		  <div class="well">
		    <form class="form-search form-inline" action="restaurantManagement.php?rM=4" method="POST">
			  <div class="form-inline">
				<legend><center>Restauracje oczekujące na akceptację</center></legend>
			    <center>			  
			    <select class="input" style="width: 200px;" name="restaurantToAcceptChooser">
<?php
$statement=$db->query('select rtaname from restaurantsToAccept');
foreach($statement as $row)
      {
?>
					<option>
<?php
          echo($row['rtaname'].'</option>');					
      }
      $statement->closeCursor();
?>	
			    </select>
			  <button type="submit" class="btn-primary" name="getData">Pobierz Dane</button>
			  <button type="submit" class="btn-primary" name="eraseData">Usuń</button>
			  </div>
			</form>
		  </div>
		<?php		  
	if(isset($_REQUEST['getData'])) {
		if(isset($_POST['restaurantToAcceptChooser'])){
			$statement=$db->query('select * from restaurantsToAccept where rtaname="'.$_POST["restaurantToAcceptChooser"].'"');
		}
		foreach($statement as $row)
		 {
			$editRestaurant->setName($row['rtaname']);
			$editRestaurant->attachToChain($row['chain']);
			$editRestaurant->setOwner($row['owner']);
			$editRestaurant->setLocation($row['location']);
		  }
			$statement->closeCursor();
	}
 
	if(isset($_REQUEST['eraseData'])) {
		if(isset($_POST['restaurantToAcceptChooser'])){
			$db->exec('delete from restaurantsToAccept where rtaname="'.$_POST["restaurantToAcceptChooser"].'"');
		}
	}
		
?>		  
		 <div class="well">
		    <form class="form-horizontal" role="form" action="restaurantManagement.php?rM=4" method="POST">
			  <div class="form-group"><br>
				<legend><center>Formularz akceptowania nowych restauracji</center></legend>
			    <label class="col-sm-2 control-label">Nazwa:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" placeholder="Nazwa Restauracji" name="rtaName" value="
<?php
	echo($editRestaurant->getName());
?>				  
				  ">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Właściciel:</label>
			    <div class="col-sm-10">
				  <input type="text" class="form-control" placeholder="Właściciel" name="rtaOwner" value="
<?php
	$statement=$db->query('select name from users where id="'.$editRestaurant->getOwner().'"');
				foreach($statement as $row)
				  {
					echo($row['name']);
				  }
				$statement->closeCursor();
?>		  				  
				  ">
		  	    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Adres:</label>
			    <div class="col-sm-10">
			      <input type="text" class="form-control" placeholder="Adres" name="rtaAddress" value="
<?php
	echo($editRestaurant->getLocation());
?>	
				  ">
			    </div>
			  </div>
			  <div class="form-group">
			    <label class="col-sm-2 control-label">Sieć restauracji:</label>
			    <div class="col-sm-10">
			      <select class="input" name="rtaChain">
				    <option>Brak</option>
<?php
$statement=$db->query('select name from chains');
foreach($statement as $row)
      {
          echo('<option>'.$row['name'].'</option>');					
      }
      $statement->closeCursor();
?>
			      </select>
			    </div>
			  </div>
			  <div class="form-group">
			    <div>
				  <center>
				    <button type="submit" class="btn btn-primary" name="addRTA">Dodaj restaurację</button>
			      </center>
				</div>
			  </div>
		    </form>
<?php
	if(isset($_REQUEST['addRTA'])) {
		if(isset($_POST['rtaChain'])){
			$statement=$db->query('select id from chains where name="'.$_POST['rtaChain'].'"');
			foreach($statement as $row)
				  {
					$editRestaurant->attachToChain($row['id']);
				  }
				$statement->closeCursor();
		}
		if(isset($_POST['rtaName'])){
			$editRestaurant->setName($_POST['rtaName']);
		}
		if(isset($_POST['rtaOwner'])){
			$statement=$db->query('select id from users where name="'.$_POST['rtaOwner'].'"');
			foreach($statement as $row)
				  {
					$editRestaurant->setOwner($row['id']);
				  }
				$statement->closeCursor();
		}
		if(isset($_POST['rtaAddress'])){
			$editRestaurant->setLocation($_POST['rtaAddress']);
		}
		#$db->query('delete from restaurantsToAccept where rtaname="'.$_POST['rtaName'].'"');
		$editRestaurant->save();
	}	
?>
		</div>
	</div>
  </div>
  </body>
  </html>
<?php
  }
}
?>